<script setup lang='ts'>
  import { reactive, ref, computed, watch } from 'vue';
  import request from '@/api/index';
  import {
    ticketTop, ticketLeft, round, room_id, ticket_num, remind,
    betsStatus, specialPool, useControlStore, useUserStore, diamond_balance
  } from '@/pinia/getter'
  import xmimg from '@/components/xmimg.vue';
  import moveTickket from './moveTickket.vue';
  const props = defineProps({
    poolId:{
      type:Number,
      default:0
    },
    price:{
      type:[Number, String],
      default:0
    },
    allPrice:{
      type:[Number , String],
      default:0
    },
    rankList:{
      type:Array<any>,
      default:()=>[]
    },
    integralType:{
      type:Number,
      default:0
    },
    integral:{
      type:Number,
      default:0
    },
  })
  const $emit = defineEmits(['change'])
  let newAllPrice = props.allPrice as number | string
  watch(props,()=>{
    if(props.allPrice > newAllPrice)cardMove()
    newAllPrice = props.allPrice
  })
  const ticketBox = ref()
  const action = reactive({
    moveTop:0,
    moveLeft:0,
    butMove:false,
    tickketList:[] as Array<number>,
  })
  const specialPoolNum = computed(()=>{
    return specialPool.value.find(res=>res.poolId == props.poolId)?.num || 0
  })
  const getMoveTime = ()=>{
    return [
      {type:1,time:'.8s'},
      {type:2,time:'.7s'},
      {type:3,time:'.6s'},
      {type:4,time:'.55s'},
      {type:5,time:'.65s'},
      {type:6,time:'.55s'},
      {type:7,time:'.4s'},
    ].find(res=> res.type == props.poolId)?.time
  }
  const handleTouchstart = ()=>{
    action.butMove = true
  }
  // 狩猎
  let num = 0
  const getUserStake = async()=>{
    // 首次押注提醒用户
    if(!remind.value) {
      $emit('change',1)
      return
    }
    if(useUserStore.userStakeLoading) return
    if(round.value > 20) return  //停止押注
    const total = diamond_balance.value + ticket_num.value
    if(props.integral > total) {
      await useUserStore.getWallet()
      const totalTow = diamond_balance.value + ticket_num.value
      if(props.integral > totalTow) {
        $emit('change',2)
        return
      }
    }
    useUserStore.userStakeLoading = true
    diamond_balance
    await request.userStake({
      poolId:props.poolId,
      room_id:room_id.value,
      integralType:props.integralType
    }).then(res=>{
      const { data={} } = res || {}
      const { error_code=0 } = data
      // console.log('props.integral', props.integral);
      useUserStore.userStakeLoading = false
      if(error_code == 4001) {
        $emit('change',2)
        return
      }
      useUserStore.setWallet(data)
      if(!betsStatus.value) useControlStore.betsStatus = true
    }).catch(()=>{
      useUserStore.userStakeLoading = false
    })
  }
  const cardMove = ()=>{
    // 卡片
    if(action.tickketList.length < 10 - props.poolId) {
      const { top=0,left=0 } = ticketBox.value.getBoundingClientRect()
      action.moveLeft = left
      action.moveTop = top
      useControlStore.ticketLocation()
      action.tickketList.push(num+=1)
    }
  }
  const endChange = (item:any = -1)=>{
    const index = action.tickketList.findIndex(res=> res == item)
    action.tickketList.splice(index,1)
  }
</script>

<template>
  <div
    :class="[
      'butBack',
      price?'selcetBut':'',
      action.butMove?'butAnimation':''
    ]"
    @click="getUserStake"
  >
    <div :class="'borderBack'">
      <!-- 
        @touchstart="handleTouchstart"
        @touchend="handleBut"
       -->
      <div class="rewardInfo">
        <div ref="ticketBox" class="ticketBox"></div>
        <div v-show="Number(price) > 0" class="ticketNum flex-align">
          <xmimg src="@/assets/images/icon3.png" class="iconImg" />
          <div class="num">{{ price }}</div>
        </div>
        <xmimg :src="`@/assets/images/cattleIcon${poolId}.png`" class="cattleIcon" />
        <dl class="prizeInfo">
          <dt>奖励</dt>
          <dd>
            <xmimg :src="`@/assets/images/numgIcon${poolId}.png`" />
          </dd>
        </dl>
      </div>
      <div class="betsInfo">
        <div class="numBox">
          <div class="total">{{ allPrice?`${Number(allPrice)>=10000?'9999+':allPrice}券`:'' }}</div>
          <div class="num" v-if="specialPoolNum > 0">{{ specialPoolNum }}局未出</div>
        </div>
        <div class="userList flex-align">
          <div v-for="item in rankList" class="userInfo margin">
            <xmimg class="portrait" :src="item.image" oss />
            <div class="num">{{ item.score }}</div>
          </div>
          <div v-for="item in 3-rankList.length" class="margin portraitNull"></div>
        </div>
      </div>
    </div>
  </div>
  <moveTickket
    v-for="item in action.tickketList"
    :key="item"
    :moveTop="action.moveTop"
    :moveLeft="action.moveLeft"
    :style="{
      '---top':`${ticketTop}px`,
      '---left':`${ticketLeft}px`,
      '---moveTop':`${action.moveTop}px`,
      '---moveLeft':`${action.moveLeft}px`,
      '---moveTime':`${getMoveTime()}`
    }"
    @endChange="endChange(item)"
  />
</template>

<style lang='scss' scoped>
  .butBack {
    overflow: hidden;
    position: relative;
    width: 82px;
    // height: max-content;
    height: 107px;
    padding: 1px;
    box-sizing: border-box;
    background: #9CCDE6;
    border-radius: 8px;
    transition: background .15s, transform .15s;
    .borderBack {
      width: 100%;
      height: 100%;
      background: #9CCDE6;
      border: 2px solid #9CCDE6;
      box-sizing: border-box;
      border-radius: 7px;
    }
    .rewardInfo {
      position: relative;
      height: 58px;
      padding-top: 4px;
      box-sizing: border-box;
      .ticketBox {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%,-50%);
        // background: red;
      }
      .ticketNum {
        position: absolute;
        top: -3px;
        right: -3px;
        padding: 1.5px 4px;
        box-sizing: border-box;
        background: #135DAD;
        border-radius: 0px 0 0px 8px;
        .iconImg {
          width: 10px;
          height: 10px;
          margin-right: 2px;
        }
        .num {
          color: #fff;
          font-size: 10px;
          font-weight: 500;
          line-height: 10px;
        }
      }
      .cattleIcon {
        position: absolute;
        left: -2px;
        width: 47px;
        height: 44px;
      }
      .prizeInfo {
        position: absolute;
        top: 18px;
        right: 3px;
        dt {
          font-size: 10px;
          font-weight: 500;
          color: #135DAD;
          line-height: 10px;
          text-align: right;
        }
        dd {
          margin-top: 5px;
          img {
            width: 30px;
            height: 21px;
          }
        }
      }
    }
    .betsInfo {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 46px;
      padding: 6px 4px 8px;
      box-sizing: border-box;
      background: #69A0DB;
      // box-shadow: inset 0px -3px 0px 0px #07471C;
      border-radius: 0px 16px 8px 8px;
      transition: background .25s , box-shadow .25s;
      .numBox {
        position: relative;
        height: 10px;
        line-height: 10px;
        font-size: 10px;
        font-weight: 500;
        .total {
          color: #177B31;
        }
        .num {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
          right: -2px;
          font-size: 8px;
        }
      }

      
      .userList {
        margin-top: 4px;
        .userInfo {
          position: relative;
          overflow: hidden;
          width: 20px;
          height: 20px;
          border-radius: 100%;
          background: #fff;
          border: 1px solid #fff;
          box-sizing: border-box;
          display: flex;
          flex-direction: column;
          .portrait, .num {
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
          }
          .portrait {
            top: 0;
            width: auto;
            height: 100%;
          }
          .num {
            bottom: -1px;
            width: 100%;
            height: 10px;
            line-height: 10px;
            color: #fff;
            font-size: 8px;
            text-align: center;
            font-weight: 500;
            margin: auto 0 0 0;
            background: rgba($color: #000, $alpha: .5);
          }
        }
        .portraitNull {
          width: 20px;
          height: 20px;
          background: url($href+'iocn11.png') left top / 100% 100% no-repeat;
        }
        .margin:nth-child(2) {
          margin: 0 5px;
        }
      }
    }
  }
  .selcetBut {
    background: #fff;
    .borderBack {
      background: #24A5E5;
      border: 2px solid #135DAD;
    }
    .betsInfo {
      background: #145EAD;
      // box-shadow: inset 0px -3px 0px 0px #07471C;
      border-radius: 0px 16px 8px 8px;
      .numBox {
        .total {
          color: #fff;
        }
      }
    }
  }
</style>